﻿window.addEventListener("load", function() { rememberExtension.init(); initOverlay(); }, false);

// Annotation and IO service
var annotationService = Components.classes["@mozilla.org/browser/annotation-service;1"]
  .getService(Components.interfaces.nsIAnnotationService);
var ioservice = Components.classes["@mozilla.org/network/io-service;1"]
  .getService(Components.interfaces.nsIIOService);

function initOverlay() {
  var menu = document.getElementById("contentAreaContextMenu");
  menu.addEventListener("popupshowing", contextPopupShowing, false);
}

function contextPopupShowing() {
  gContextMenu.showItem("setRememberAnno", !currentIsRemembered());
  gContextMenu.showItem("removeRememberAnno", currentIsRemembered());
}

// This is the main thingie
var rememberExtension = {
  init: function() {
    var appcontent = document.getElementById("appcontent");   // browser
    if(appcontent)
      appcontent.addEventListener("load", this.onPageLoad, true);
    var messagepane = document.getElementById("messagepane"); // mail
    if(messagepane)
      messagepane.addEventListener("load", this.onPageLoad, true);
  },

  onPageLoad: function(aEvent) {
    var doc = aEvent.originalTarget;
    var current = doc.location.href;
    var justnow = Date.now();
 
    try {
      var uri = ioservice.newURI(current, null, null);
      var count = annotationService.getAnnotationInt64(uri, "remember_to_surf/count");
      count = count + 1;
      annotationService.setAnnotationInt64(uri, "remember_to_surf/count", count, 0, 0);
      annotationService.setAnnotationInt64(uri, "remember_to_surf/last", justnow, 0, 0);
    } catch(e) {
      // Do nooooothing!
    }
  }
}

// This is the function for creating a ranking list and displaying it
function doVooDoo() {
  var justnow = Date.now();
  var theseSites = new Array();
  var pages = annotationService.getPagesWithAnnotation("remember_to_surf/count", {});
  
  for (var i = 0; i < pages.length; i++) {
    var uri = pages[i];
    var first = annotationService.getAnnotationInt64(uri, "remember_to_surf/first");
    var last = annotationService.getAnnotationInt64(uri, "remember_to_surf/last");
    var count = annotationService.getAnnotationInt64(uri, "remember_to_surf/count");
    
    // Only display if has more then one visit or is over 7 days old
    if (count != 1 && first < (justnow - 604800)) {
      var average = (last - first) / count;
      var sincelast = justnow - last;
      var rank = (sincelast / average) * 1000;
      
      rank = Math.round(rank);
      
      // THIS REALLY SUCKS!
      if (rank < 10) {
        rank = "0000000" + rank;
      } else if (rank < 100) {
        rank = "000000" + rank;
      } else if (rank < 1000) {
        rank = "00000" + rank;
      } else if (rank < 10000) {
        rank = "0000" + rank;
      } else if (rank < 100000) {
        rank = "000" + rank;
      } else if (rank < 1000000) {
        rank = "00" + rank;
      } else if (rank < 10000000) {
        rank = "0" + rank;
      }
      
      var currently = new Array();
      currently[0] = rank;
      currently[1] = uri.spec;
      currently[2] = count;
      theseSites.push(currently);
    }
  }
  
  theseSites = theseSites.sort();
  theseSites = theseSites.reverse();
  
  var everything = "<table border='1' width='100%'><tr><td>Rank</td><td>Visits</td><td>URL</td></tr>\n";
  
  for (var key in theseSites) {
    everything = everything + "<tr><td><b>" + theseSites[key][0] + "</b></td><td>"+ theseSites[key][2] + "</td><td><a href='" + theseSites[key][1] + "'>" + theseSites[key][1] + "</a></td></tr>";
  }
  
  everything = everything + "</table>";
 
  var savefile = Components.classes["@mozilla.org/file/directory_service;1"]
    .getService(Components.interfaces.nsIProperties)
    .get("ProfD", Components.interfaces.nsIFile);
  savefile.append("remembertosurf.html");
  
  var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
    .createInstance(Components.interfaces.nsIFileOutputStream);
  foStream.init(savefile, 0x02 | 0x08 | 0x20, 0777, 0);
  foStream.write(everything, everything.length);
  foStream.close();

  getBrowser().loadURI(savefile.path);
}

// Set Annotation
function setRememberAnno() {
  var current = getBrowser().currentURI;
  var justnow = Date.now();
  annotationService.setAnnotationInt64(current, "remember_to_surf/first", justnow, 0, 0);
  annotationService.setAnnotationInt64(current, "remember_to_surf/last", justnow, 0, 0);
  annotationService.setAnnotationInt64(current, "remember_to_surf/count", 1, 0, 0);
}

// Remove Annotation
function removeRememberAnno() {
  var current = getBrowser().currentURI;
  annotationService.removeAnnotation(current, "remember_to_surf/first");
  annotationService.removeAnnotation(current, "remember_to_surf/last");
  annotationService.removeAnnotation(current, "remember_to_surf/count");
}

function currentIsRemembered() {
  var current = getBrowser().currentURI;
  if (annotationService.hasAnnotation(current, "remember_to_surf/count")) {
    return true;
  } else {
    return false;
  }
}